/*
 * @Author: LOOP oo
 * @Date: 2025-03-29 11:24:59
 * @LastEditors: Joel
 * @LastEditTime: 2025-03-31 09:24:41
 * @FilePath: \loop2\usr\src\main.c
 * @Description:
 *
 * Copyright (c) 2025 by LOOP oo, All Rights Reserved.
 */

#include <stdio.h>
#include "main.h"
#include "SEGGER_RTT.h"
/* 系统时钟 */
static void APP_SystemClockConfig(void);

/* 按键处理 */
static uint8_t delay_switch = 0;
static void button_callback(void *arg);
/* 马达处理 */
static void motor_callback(void *arg);
/* task */
static void power_callback(void *arg);

int main(void)
{
  APP_SystemClockConfig();

  /* rtt初始化打印 */
  SEGGER_RTT_Init();
  printf("LOOP2\r\nClock: %ld\r\n", SystemCoreClock);

  /* 电源初始化 */
  power_init();
  set_vcc_enable(1);
  /* 初始化马达 */
  motor_init();
  /* 初始化按键 */
  button_init();
  /* 电池检测 */
  battery_detect_init();
  /* 定时器初始化 */
  User_Timer_Init();

  /* 按键处理进程20ms */
  User_Timer_Config(TIMERID0, BASE_KEY_TIME, button_callback, NULL);
  User_Timer_Start(TIMERID0);

  /* task进程 */
  User_Timer_Config(TIMERID1, BASE_POWER_TIME, power_callback, NULL);
  User_Timer_Start(TIMERID1);

  /* motor进程 */
  User_Timer_Config(TIMERID2, BASE_MOTER_TIME, motor_callback, NULL);
  User_Timer_Start(TIMERID2);

  printf("\n\r("__DATE__
                   " - " __TIME__ ")");
  printf("\r\nFirmware_version = %d\r\n", (int)UPDATE_VERSION);

  while (1)
  {
    printf("echo\r\n");
    LL_mDelay(500);
  }
}

static void APP_SystemClockConfig(void)
{
  LL_UTILS_ClkInitTypeDef UTILS_ClkInitStruct;

  LL_RCC_HSI_Enable();
  /* Change this value to adjust clock frequency, larger is faster */
  LL_RCC_HSI_SetCalibFreq(LL_RCC_HSICALIBRATION_24MHz + 15);
  while (LL_RCC_HSI_IsReady() != 1)
    ;

  UTILS_ClkInitStruct.AHBCLKDivider = LL_RCC_SYSCLK_DIV_1;
  UTILS_ClkInitStruct.APB1CLKDivider = LL_RCC_APB1_DIV_1;
  LL_PLL_ConfigSystemClock_HSI(&UTILS_ClkInitStruct);

  /* Re-init frequency of SysTick source, reload = freq/ticks = 48000000/1000 = 48000 */
  LL_InitTick(48000000, 1000U);

  return;
}

/**
 * @description: 按键回调
 * @param {void} *arg
 * @return {*}
 */
static void button_callback(void *arg)
{
  /* 按键滤波延迟 */
  static uint8_t delay_filter = 0;
  /* 双击事件 */
  static uint8_t pressed_check = 0;
  static uint8_t pressed_index = 0;

  /* 获取GPIO电平 */
  int value = LL_GPIO_IsInputPinSet(GPIOA, LL_GPIO_PIN_7);

  /* 触发中断 */
  if (delay_switch != get_button())
  {
    ++delay_filter;
    /* 单击/双击 */
    if ((BUTTON_CLICK_TIME == delay_filter) && (!value))
    {
      pressed_check ++;
      printf("pressed_check = %d\n", pressed_check);

    }/* 长按 */
    else if ((BUTTON_LONG_TIME < delay_filter) && (!value))
    {
      pressed_check = 0;
      printf("long_pressed= %d\n", value);
    }/* 释放 */
    else if (1 == value)
    {
      delay_switch = get_button();
      delay_filter = 0;
      printf("button_Released= %d\n", value);
    }
  }
  else
  {
    delay_filter = 0;
  }/* 双击释放 */
  if(pressed_check)
  if (BUTTON_PRESSED_TIME < ++pressed_index)
  {
    pressed_index = 0;
    pressed_check = 0;
  }

  return;
}
/**
 * @description: power_callback回调
 * @param {void} *arg
 * @return {*}
 */
static void power_callback(void *arg)
{
  // static 关键字tmpflag不被清除
  static int tmpflag = 1;
  if (++tmpflag == 10)
  {
    tmpflag = 0;
    printf("get_charging_state = %d\n", get_charging_state());
    printf("get_battery_voltage = %ld\n", get_battery_voltage());
  }
  return;
}
/**
 * @func: 
 * @description: moter进程回调
 * @param {void} *arg
 * @return {*}
 * @example: 
 */
static void motor_callback(void *arg)
{
  static uint32_t set_motor_time = 0;
  static uint8_t motor_flag;

  if (RUN_MOTER_TIME == ++set_motor_time)
  {
    set_motor_time = 0;
    motor_flag = !motor_flag;
    set_motor_run(motor_flag);
    printf("set_motor_ = %d\n", motor_flag);
  }

  return;
}
void APP_ErrorHandler(void)
{
  while (1)
    ;
}

#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{
  while (1)
    ;
}
#endif /* USE_FULL_ASSERT */
